import 'dart:io';

import 'package:demo/utils/log_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import 'app/global.dart';
import 'app_binding.dart';
import 'theme/app_colors.dart';
import 'theme/app_text_theme.dart';
import 'utils/sp_util.dart';

void main() async {
  const String channel = String.fromEnvironment('APP_CHANNEL', defaultValue: 'APP001');

  Global.CHANNEL = channel;

  await GetStorage.init(SpUtil.spKey);
  await Global.init();

  runApp(const MyApp());
  configLoading();

  if (Platform.isAndroid) {
    var systemUiOverlayStyle = const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context , child) {
        return GetMaterialApp(
          // title: 'Flutter Demo',
          theme: ThemeData(
            appBarTheme: const AppBarTheme(
              backgroundColor: Colors.white,
              iconTheme: IconThemeData(color: Color_131732),
              centerTitle: true,
              elevation: 0,
              scrolledUnderElevation: 0.0, // 不然滑动的时候可能到时appbarb背景颜色变成页面的背景颜色
            ),
          ),
          debugShowCheckedModeBanner:false, // 隐藏debug标识
          initialRoute:AppPages.INITIAL,
          getPages:AppPages.routes,
          initialBinding: AppBinding(),
          builder: EasyLoading.init(builder: (context, widget) {
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling), /// 设置文字大小不随系统设置改变
              child: widget!,
            );
          }),
          defaultTransition: Transition.noTransition, // https://blog.csdn.net/yang_6799/article/details/131966520
        );
      },
    );
  }
}

void configLoading() {
  EasyLoading.instance
    ..contentPadding = const EdgeInsets.symmetric(horizontal: 12,vertical: 6)
    // ..toastPosition = EasyLoadingToastPosition.center
    ..displayDuration = const Duration(milliseconds: 1000)
    ..radius = 6
    ..loadingStyle = EasyLoadingStyle.custom
    ..backgroundColor = Colors.black38 // 只能在loadingStyle为custom下生效
    ..boxShadow = [const BoxShadow(color: Colors.black38)] // 和backgroundColor一起使用
    ..indicatorColor = Colors.blue
    ..textColor = Colors.white
    ..indicatorSize = 60
    ..indicatorWidget = const SizedBox(width: 70,height: 82,child: CupertinoActivityIndicator(color: Colors.white, radius: 15))
    // ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    // ..indicatorSize = 45.0
    // ..progressColor = Colors.yellow
    // ..maskColor = Colors.blue.withOpacity(0.5)
    // ..userInteractions = false
    // ..dismissOnTap = false
    // ..customAnimation = CustomAnimation()
  ;
}
